package db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import model.Guest;
import model.RoomType;

public class DbRoomType {
	private Connection con;
	
	public DbRoomType() {
		con = DbConnection.getInstance().getDBcon();
	}
	
	public RoomType findRoomType(int typeId) {
			String query = "Id = " + typeId;
			return singleWhere(query);
		}
	
	//Singelwhere is used when we only select one employee 	
		private RoomType singleWhere(String wClause)
		{
			ResultSet results;
			RoomType empObj = new RoomType();
	              
		        String query =  buildQuery(wClause);
	              System.out.println(query);
			try{ // read the employee from the database
		 		Statement stmt = con.createStatement();
		 		stmt.setQueryTimeout(5);
		 		results = stmt.executeQuery(query);
		 		
		 		if( results.next() ){
	                          empObj = buildRoomType(results);
	                          //assocaition is to be build
	                          stmt.close();
	                        
				}
	                      else{ //no employee was found
	                          empObj = null;
	                      }
			}//end try	
		 	catch(Exception e){
		 		System.out.println("Query exception: "+e);
		 	}
			return empObj;
		}
		
		private String buildQuery(String wClause)
		{
		    String query="SELECT id, roomType, price  FROM RoomType";
			
			if (wClause.length()>0)
				query=query+" WHERE "+ wClause;
				
			return query;
		}
		
		private RoomType buildRoomType(ResultSet results)
		{   RoomType oObj = new RoomType();
		    try{ // the columns from the table emplayee  are used
		    	oObj.setId(results.getInt("id"));
		    	oObj.setType(results.getString("roomType"));
		    	oObj.setPrice(results.getInt("price"));
		    	
		    	
		          
		    }
		   catch(Exception e)
		   {
		       System.err.println("error in building the product object");
		   }
		   return oObj;
		}

		/**public RoomType findType(int roomTypeId) {
			String query = "id = " + roomTypeId;
			return singleWhere(query);
		}*/
		
		

}
